/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.switz.agendacita.persistence;


import com.switz.agendacita.dto.PersonalNoHorario;
import com.switz.agendacita.persistence.iface.PersonalNoHorarioDao;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jose angel
 */
@Repository
public class PersonalNoHorarioDaoImpl extends JdbcDaoSupport implements PersonalNoHorarioDao {

    @Autowired
    public PersonalNoHorarioDaoImpl(DataSource dataSource) {

        setDataSource(dataSource);
    }

    @Override
    public List getHoraInicioHoraFinReservacion(String idPersonal, int dia, Date fechaInicio, Date fechaFin) {
  
        String query = "SELECT horainicio,horafin FROM personalnohorarios where idpersonal=? and dia=? "
                + "or (idpersonal=? and fechainicio<=? and fechafin>=?)";
             
        List personalHorarioExcepcionList = getJdbcTemplate().query(query, new Object[]{idPersonal, dia, idPersonal, fechaInicio, fechaFin},
                ParameterizedBeanPropertyRowMapper.newInstance(PersonalNoHorario.class));
        
        return personalHorarioExcepcionList;


    }

  

    @Override
    public void deleteDiasConHorariosDescanzoPorIdPersonal(String idEmpresa,String idPersonal) {
        String query = "delete from personalnohorarios where idempresa=? and idpersonal =? and (fechainicio is null and fechafin is null);";
        getJdbcTemplate().update(query, new Object[]{idEmpresa,idPersonal});
    }

    @Override
    public void insertBatchHorarioDescanzoParaDiasLaboralesPorIdPersonal(List personalHorarioExcepcionsList) {


        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(
                getJdbcTemplate().getDataSource())
                .withTableName("personalnohorarios");

         SqlParameterSource[] array =SqlParameterSourceUtils.createBatch(personalHorarioExcepcionsList.toArray());

       

        simpleJdbcInsert.executeBatch(array);
    }

	@Override
	public void insertPeriodoDescanzo(PersonalNoHorario personalNoHorario) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(
                getJdbcTemplate().getDataSource())
                .withTableName("personalnohorarios");
           SqlParameterSource parameters = new BeanPropertySqlParameterSource(personalNoHorario);
           simpleJdbcInsert.execute(parameters);
		
	}

	@Override
	public void deletePeriodoDescanzo(PersonalNoHorario personalNoHorario) {
		String query = "delete from personalnohorarios "
				+ " where idempresa=? and "
				+ " idpersonal=? and "
				+ " fechainicio=? and "
				+ " fechafin=? and "
				+ " horainicio=? and "
				+ " horafin=?";
        getJdbcTemplate().update(query, new Object[]{personalNoHorario.getIdEmpresa(),
        		personalNoHorario.getIdPersonal(),personalNoHorario.getFechaInicio(),
        		personalNoHorario.getFechaFin(),personalNoHorario.getHoraInicio(),personalNoHorario.getHoraFin()});
	}
	
	@Override
	public void insertPeriodoVacacional(PersonalNoHorario personalNoHorario) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(
                getJdbcTemplate().getDataSource())
                .withTableName("personalnohorarios");
           SqlParameterSource parameters = new BeanPropertySqlParameterSource(personalNoHorario);
           simpleJdbcInsert.execute(parameters);
		
	}
	
	@Override
	public void deletePeriodoVacacional(PersonalNoHorario personalNoHorario) {
		String query = "delete from personalnohorarios "
				+ " where idempresa=? and "
				+ " idpersonal=? and "
				+ " fechainicio=? and "
				+ " fechafin=? and "
				+ " horainicio is null and "
				+ " horafin is null";
        getJdbcTemplate().update(query, new Object[]{personalNoHorario.getIdEmpresa(),
        		personalNoHorario.getIdPersonal(),personalNoHorario.getFechaInicio(),
        		personalNoHorario.getFechaFin()});
	}
	
	
}
